using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;

using Sedna.Core.Controls.Support;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controller;

namespace Sedna.Core.Controls
{
	/// <summary>
	/// Localized label.
	/// </summary>
	public class Label : BaseLabel, ILabelledControl
	{
		private string textKey;
	    
		public virtual string TextKey
		{
			get { return textKey; }
			set { textKey = value; }
		}

        private string toolTipKey;
        public virtual string ToolTipKey
        {
            get { return toolTipKey; }
            set { toolTipKey = value; }
        }

        public string LocalizedToolTip
        {
            get
            {
                if (Configuration.DataTranslator != null && Configuration.DataTranslator.GetType() != typeof(TrivialDataTranslator))
                {
                    return ToolTip;
                }
                return Configuration.GetLocalizedPostfixLabel(ToolTipKey, ToolTip);
            }
        }

		/// <summary>
		/// Needed for filter state javascript generation. 
		/// </summary>
		public string LocalizedText
		{
			get
			{
				if (Configuration.DataTranslator!=null&&Configuration.DataTranslator.GetType()!=typeof(TrivialDataTranslator))
				{
					return Text;	
				}
				return Configuration.GetLocalizedPostfixLabel(TextKey, Text);
			}
		}

	    
	    public Label() :base()
 		{
			configurationId="configuration.default.Label";
 		}

        public Label(IConfiguration configuration) : base(configuration)
		{
		}

		protected override void Build()
		{
			base.Build();
		    
			this.TextKey = configuration.TextKey;						
			this.Style.Add("overflow", "hidden");
		}


		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			Text = LocalizedText;
            ToolTip = LocalizedToolTip;
		}
		
		
		protected override void Bind()
		{
		   this.Visible = configuration.Visible;
			
			binder.Bind(new string[]{"Visible", "TextKey"});

			object text;
			if (binder.GetPropertyValue("Text", out text))
			{
				text=configuration.DataTranslator.Translate(this, text);
				Text=text!=null?text.ToString():String.Empty;
			}						
			this.Visible = this.Visible && configuration.IsVisibleForPrincipal();

            ToolTip = Configuration.ToolTip;
            ToolTipKey = Configuration.ToolTipKey;
            binder.Bind("ToolTip");
            binder.Bind("ToolTipKey");

		}

		protected override void Unbind()
		{
		   binder.Unbind(new string[] { "Text", "TextKey", "Visible" });
           binder.Unbind("ToolTip");
           binder.Unbind("ToolTipKey");

		}

	}
}
